package main

func main() {

	l5 := ListNode{
		Val:  5,
		Next: nil,
	}

	l4 := ListNode{
		Val:  4,
		Next: &l5,
	}
	l3 := ListNode{
		Val:  3,
		Next: &l4,
	}

	l2 := ListNode{
		Val:  2,
		Next: &l3,
	}

	l1 := ListNode{
		Val:  1,
		Next: &l2,
	}

	PrintList(&l1)
	r := reverseList(&l1)
	PrintList(r)
}

func reverseList(head *ListNode) *ListNode {

	var prev *ListNode

	curr := head
	for curr != nil {
		next := curr.Next
		curr.Next = prev
		prev = curr
		curr = next
	}

	return prev

}

type ListNode struct {
	Val  int
	Next *ListNode
}

func PrintList(l *ListNode) {

	for l != nil {
		print(l.Val, " ")
		l = l.Next
	}

	println("")
}
